#import "SFUContact.h"

/**
 * An SFUContact is an object that represents a single contact at SFU.
 *
 * Conforms to the PersistentDataType protocol.
 *
 * @author      James Lin
 * @version     1.0.5
 * @since       1.0.5
 * @see         PersistentDataType
 */
@implementation SFUContact

@synthesize lastName;
@synthesize firstName;
@synthesize jobTitle;
@synthesize phoneDict;
@synthesize roomDict;
@synthesize email;
@synthesize aboutURL;

/**
 * Initialize the SFU contact with complete details.
 *
 * @param theLastName   the contact's last name
 * @param theFirstName  the contact's first name
 * @param theJobTitle   the contact's job title
 * @param thePhoneDict  the phone numbers of the contact
 * @param theRoomDict   the room numbers of the contact
 * @param theEmail      the contact's email
 * @param theAboutURL   the contact's optional about URL
 * @return              the initialized SFUContact
 */
- (id) initWithLastName:(NSString*)theLastName 
			  firstName:(NSString*)theFirstName 
			   jobTitle:(NSString*)theJobTitle 
			  phoneDict:(NSMutableDictionary*)thePhoneDict 
			   roomDict:(NSMutableDictionary*)theRoomDict 
				  email:(NSString*)theEmail 
			   aboutURL:(NSString*)theAboutURL {
	if (self = [super init]) {
		self.lastName = theLastName;
		self.firstName = theFirstName;
		self.jobTitle = theJobTitle;
		self.phoneDict = thePhoneDict;
		self.roomDict = theRoomDict;
		self.email = theEmail;
		self.aboutURL = theAboutURL;
	}
	return self; 
} 

/**
 * Compute a unique key for the contact.
 *
 * @return  the unique key
 */
- (NSString*)getKey {
	return [NSString stringWithFormat:@"%@ %@ %@ %@", 
			lastName, firstName, jobTitle, email];
}

/**
 * Encodes the SFUContact for archiving options.
 *
 * Supports only keyed archiving.
 *
 * @param coder  the NSCoder used for encoding
 */
- (void)encodeWithCoder:(NSCoder*)coder {
	if ([coder allowsKeyedCoding]) {
		[coder encodeObject:lastName forKey:@"SFUContactLastName"];
		[coder encodeObject:firstName forKey:@"SFUContactFirstName"];
		[coder encodeObject:jobTitle forKey:@"SFUContactJobTitle"];
		[coder encodeObject:phoneDict forKey:@"SFUContactPhoneDict"];
		[coder encodeObject:roomDict forKey:@"SFUContactRoomDict"];
		[coder encodeObject:email forKey:@"SFUContactEmail"];
		[coder encodeObject:aboutURL forKey:@"SFUContactAboutURL"];
	}
}

/**
 * Initializes the SFUContact from archive.
 *
 * Supports only keyed archiving.
 *
 * @param coder  the NSCoder used for decoding
 * @return       the SFUContact decoded from archive
 */
- (id)initWithCoder:(NSCoder*)coder {
	if (!(self = [super init])) {
		return nil;
	}
	
	if ([coder allowsKeyedCoding]) {
		self.lastName = [coder decodeObjectForKey:@"SFUContactLastName"];
		self.firstName = [coder decodeObjectForKey:@"SFUContactFirstName"];
		self.jobTitle = [coder decodeObjectForKey:@"SFUContactJobTitle"];
		self.phoneDict = [coder decodeObjectForKey:@"SFUContactPhoneDict"];
		self.roomDict = [coder decodeObjectForKey:@"SFUContactRoomDict"];
		self.email = [coder decodeObjectForKey:@"SFUContactEmail"];
		self.aboutURL = [coder decodeObjectForKey:@"SFUContactAboutURL"];
	}
	
	return self;
}

/**
 * Facilitate alphabetic sorting by last name of contact.
 */
- (NSComparisonResult)compareLastName:(id)otherContact {
	return [self.lastName compare:((SFUContact*)otherContact).lastName];
}

/**
 * Deallocate the SFU contact.
 */
- (void)dealloc {
	[lastName release]; 
	lastName = nil;
	[firstName release]; 
	firstName = nil;
	[jobTitle release]; 
	jobTitle = nil;
	[phoneDict release]; 
	phoneDict = nil;
	[roomDict release]; 
	roomDict = nil;
	[email release]; 
	email = nil;
	[aboutURL release]; 
	aboutURL = nil;
    [super dealloc];
}

@end